package com.yxzr.youyou.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yxzr.youyou.R;
import com.yxzr.youyou.bean.LeaveMessageBean;
import com.yxzr.youyou.utils.DateTimeUtil;

import java.util.ArrayList;
import java.util.List;

public class LeaveMessageAdapter extends RecyclerView.Adapter<LeaveMessageAdapter.ViewHolder> {
    private Context mContext;
    private List<LeaveMessageBean.DataBean> list = new ArrayList<>();
    public LeaveMessageAdapter(Context context){
        this.mContext = context;
    }
    public void addList(List<LeaveMessageBean.DataBean> mlist){
        if (mlist != null && mlist.size() > 0){
            list.addAll(mlist);
            notifyDataSetChanged();
        }
    }

    public void clearList(){
        list.clear();
        notifyDataSetChanged();
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ViewHolder viewHolder = new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.leavemessage_adapter_layout,parent,false));
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        if (!TextUtils.isEmpty(list.get(position).getAdd_time())){
            holder.leavemessage_tv_time.setText(DateTimeUtil.formatDateTime((Long.parseLong(list.get(position).getAdd_time()) * 1000),"yyyy/MM/dd HH:mm:ss"));
        }
        holder.leavemessage_tv_content.setText(list.get(position).getContent());
        if (TextUtils.isEmpty(list.get(position).getHuifu())){
            holder.leavemessage_tv_admin.setVisibility(View.GONE);
        }else {
            holder.leavemessage_tv_admin.setVisibility(View.VISIBLE);
            holder.leavemessage_tv_admin.setText("管理员回复："+list.get(position).getHuifu());
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        private TextView leavemessage_tv_time,leavemessage_tv_content,leavemessage_tv_admin;
        public ViewHolder(View itemView) {
            super(itemView);
            leavemessage_tv_time = itemView.findViewById(R.id.leavemessage_tv_time);
            leavemessage_tv_content = itemView.findViewById(R.id.leavemessage_tv_content);
            leavemessage_tv_admin = itemView.findViewById(R.id.leavemessage_tv_admin);
        }
    }
}
